package org.chen.auth.service.impl;


import org.chen.auth.dto.LoginDTO;
import org.chen.auth.service.ILoginService;
import org.chen.auth.vo.LoginVO;
import org.chen.bas.context.LoginContext;
import org.chen.org.domain.Employee;
import org.chen.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private IEmployeeService service;
    @Override
    public LoginVO login(LoginDTO loginDTO) {

        // 先根据 账号查 对象
        Employee employee = service.selectByName(loginDTO.getAccount());
        if (Objects.isNull(employee)){
            throw new RuntimeException("账号或密码错误");
        }
        // 判断对象中的密码 是否匹配
        if (!employee.getPassword().equals(loginDTO.getCheckPass()))
            throw new RuntimeException("账号或密码错误");
        // 如果匹配 生成token
        String token = UUID.randomUUID().toString();

        LoginVO loginVO = new LoginVO(token, employee);
        LoginContext.loginMap.put(token,employee);
        // 封装VO 返回
        return loginVO;
    }
}
